package com.jk.service.account;

import com.jk.dao.account.AccPermissionDao;
import com.jk.dao.account.AccRoleDao;
import com.jk.dao.account.AccRolePermissionDao;
import com.jk.entity.account.AccPermission;
import com.jk.entity.account.AccRole;
import com.jk.entity.account.AccRolePermission;
import com.jk.util.AccountUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.dao.DataAccessException;

import java.util.*;

/**
 * ACC_ROLE service
 * Tue Sep 22 11:02:19 CST 2015 yanghuan
 */
@Service
public class AccRoleService {

    @Autowired
    private AccRoleDao accRoleDao;
    @Autowired
    private AccPermissionDao accPermissionDao;
    @Autowired
    private AccRolePermissionDao accRolePermissionDao;

    public int insert(AccRole accRole) throws DataAccessException {
        return accRoleDao.insert(accRole);
    }

    public int delete(String accMenuId) throws DataAccessException {
        return accRoleDao.deleteByPrimaryKey(accMenuId);
    }

    public List<AccRole> getRoleList() throws DataAccessException {
        List<AccRole> roles = accRoleDao.selectAll();
        if (null != roles) {
            return roles;
        } else {
            return new ArrayList<AccRole>();
        }
    }

    //获取角色显示权限
    public List<Map<String, Object>> getPermissions(String accRoleId) throws DataAccessException {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        //角色是否存在
        AccRole role = accRoleDao.selectByPrimaryKey(accRoleId);
        if (role != null) {
            //获取所有权限
            List<AccPermission> permissionList = accPermissionDao.selectAll();
            Set<String> permissionIdSet = accRolePermissionDao.getPermissionIdSetByRoleId(accRoleId);
            if (null == permissionList) {
                permissionList = new ArrayList<AccPermission>();
            }
            if (null == permissionIdSet) {
                permissionIdSet = new HashSet<String>();
            }
            return AccountUtil.getShowPermissions(permissionIdSet, permissionList, "root");
        } else {
            return mapList;
        }
    }

    @Transactional(value = "account", rollbackFor = Exception.class)
    public void grantPermissions(String accRoleId, String[] perIdArray) throws DataAccessException {
        //获取所有权限
        List<AccPermission> permissions = accPermissionDao.selectAll();
        if (null == permissions) {
            permissions = new ArrayList<AccPermission>();
        }
        Set<String> perIdSet = new HashSet<String>();
        for (String id : perIdArray) {
            perIdSet.add(id);
            List<String> childrenPermissionIds = AccountUtil.getChildrenPermissionIds(id, permissions);
            perIdSet.addAll(childrenPermissionIds);
        }
        //授权
        for (String perId : perIdSet) {
            AccRolePermission rolePermission = new AccRolePermission();
            rolePermission.setAccRolePermissionId(UUID.randomUUID().toString());
            rolePermission.setAccPermissionId(perId);
            rolePermission.setAccRoleId(accRoleId);
            accRolePermissionDao.insert(rolePermission);
        }
        //删除角色权限
        AccRolePermission accRolePermission = new AccRolePermission();
        accRolePermission.setAccRoleId(accRoleId);
        accRolePermissionDao.delete(accRolePermission);
    }

}

